(function ($) {
    "use strict"
    var baseUrl = "http://192.168.2.120:8021"
    var element = angular.element;
    angular.module("apabiBook", ["ngRoute"])
            .config(function ($routeProvider) {
                $routeProvider.when("/", {templateUrl: "template/books.html", controller: "IndexController"})
                $routeProvider.when("/book/:id", {templateUrl: "template/book.html", controller: "BookController"})
            })
            .controller("IndexController", function ($scope, $http, $window) {
                $scope.pageIndex = $scope.pageIndex || 1;
                $scope.isLoad = true;
                getBooks(function (res) {
                    $scope.books = res;
                });
                sroll()


                function sroll() {
                    $($window).bind("scroll", function (e) {
                        var maxHeight = ($(".books").height() - $($window).height()) * 0.8;
                        var height = $($window).scrollTop();
//                        console.log({maxHeight: maxHeight, height: height})
                        if (height > maxHeight && $scope.isLoad && $scope.pageIndex <= 12) {
                            $scope.pageIndex += 1;
                            getBooks(function (res) {
                                console.log(res)
                                $scope.books = $scope.books.concat(res);
                                console.log($scope.books)

                            });
                        }
                    })
                }
                function getBooks(callback) {
                    $scope.isLoad = false;
                    $http.jsonp(baseUrl + "/api/newList.jsonp?page=" + $scope.pageIndex + "&pageSize=10&callback=JSON_CALLBACK").success(function (res) {
                        $scope.isLoad = true;
                        callback(res);
                    })
                }
            })
            .controller("BookController", function ($scope, $http, $routeParams) {
                console.log("In BookController id:" + $routeParams.id)
                var bookId = $routeParams.id;
                $http.jsonp(baseUrl + "/api/getbook.jsonp?ztf=" + bookId + "&&callback=JSON_CALLBACK").success(function (res) {
                    console.log(res);
                })

            })
})(jQuery);

